﻿using System;
using TravelAgency.DAL;
using Telerik.OpenAccess;
using Telerik.OpenAccess.FetchOptimization;

namespace TravelAgency.AspNet.Agents
{
    public partial class ViewReservationsOfTrip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tripIdString = this.Request.QueryString["tripId"];
            int tripId = int.Parse(tripIdString);
            this.Session["tripId"] = tripId;
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                Trip trip = dao.GetTripByTripId(tripId);
                this.LabelTripTitle.Text = this.Server.HtmlEncode(trip.Title);
            }
        }

        protected void LinkButtonYas_Click(object sender, EventArgs e)
        {
            string reservationIdString =
                this.RadListViewReservations.SelectedItems[0].GetDataKeyValue("ReservationId").ToString();
            int reservationId = int.Parse(reservationIdString);
            int tripId = (int)this.Session["tripId"];
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                string peopleCountString =
                    this.RadListViewReservations.SelectedItems[0].GetDataKeyValue("PeopleCount").ToString();
                int peopleCount = int.Parse(peopleCountString);
                Trip trip = dao.GetTripByTripId(tripId);
                trip.CurrentPeopleCapacity -= peopleCount;
                dao.DeleteReservationByReservationId(reservationId);
                dao.SaveChanges();
            }

            this.Response.Redirect("~/Agents/ViewReservationsOfTrip.aspx?tripId=" + tripId.ToString());
        }

        protected string UserNameWordWrapEval(string PropertyName)
        {
            string fullUserName = Eval(PropertyName).ToString();
            if (fullUserName.Length > 50)
            {
                string shortTitle = fullUserName.Substring(0, 50);
                return shortTitle + "...";
            }
            else
            {
                return fullUserName;
            }
        }

        protected void LinkButtonSearch_Click(object sender, EventArgs e)
        {
            string searchPhrase = this.TextBoxSearch.Text;
            this.RadListViewReservations.FilterExpressions.Clear();
            this.RadListViewReservations.FilterExpressions.BuildExpression().
                Contains("Aspnet_Membership.Aspnet_User.UserName", searchPhrase).Build();
            this.RadListViewReservations.Rebind();
        }

        protected void OpenAccessDataSourceReservations_ContextCreating(
            object sender, OpenAccessDataSourceContextCreatingEventArgs e)
        {            
        }
    }

    //public partial class TravelAgencyModel : OpenAccessContext
    //{
    //    public IObjectScope GetInternalScope()
    //    {
    //        return this.GetScope();
    //    }
    //}
}